public with sharing class MC_LeaderViewController
{

    public PageReference invite() {
        return null;
    }

    public Missional_Community__c CurrentGroup
    {
        get
        {
            if(CurrentGroup==null)
            {
                String arenaGroupId=ApexPages.currentPage().getParameters().get('group');
                if(arenaGroupId==null||arenaGroupId=='')
                    return null;
                CurrentGroup=GetGroupByArenaId(arenaGroupId);
            }

            return CurrentGroup;
        }
        
        set;
    }
    
    public Missional_Community_Member__c MemberToUpdate {get; set;}
    
    
    public String SingleQuote
    {
        get
        {
            return '\'';
        }
    }
    
    public PageReference sendMassEmail()
    {
        return null;
    }

    public List<Missional_Community_Member__c> getMembers()
    {
        if(CurrentGroup==null)
        {
            System.Debug('No Arena group id available');
            return null;
        }
        String groupId=CurrentGroup.Id;
        //Go ahead and generate this every time--no paging support here. See MC_Filter for paging if necessary
        List<Missional_Community_Member__c> members=
            [
                select
                    id
                    ,contact__r.Name
                    ,contact__r.gender__c
                    ,contact__r.Phone
                    ,contact__r.Email
                    ,date_joined__c
                    ,has_been_contacted__c
                    ,interested_in_joining__c
                    ,invitation_sent__c
                from
                    Missional_Community_Member__c
                where
                    missional_community__c=:groupId
                    and role__c<>'Leader'
                order by
                    date_joined__c
            ];    //XYZZY todo add dynamic sorting
        return members;
    }
    
    public Integer getNumMembers()
    {
        List<Missional_Community_Member__c> members=getMembers();
        if(members==null)
            return 0;
        return getMembers().size();
    }
    
    public PageReference startEdit()
    {
        String memberId=ApexPages.currentPage().getParameters().get('editId');
        MemberToUpdate=[select id, has_been_contacted__c, interested_in_joining__c from Missional_Community_Member__c where Id=:memberId];
        return null;
    }
    
    public PageReference cancelEdit()
    {
        MemberToUpdate=null;
        return null;
    }
    
    public PageReference saveEdit()
    {
        //If they update Interested In Joining, set Has Been Contacted to true for them
        if(MemberToUpdate.Interested_In_Joining__c!=null&&MemberToUpdate.Interested_In_Joining__c!='Haven\'t yet responded')
            MemberToUpdate.Has_Been_Contacted__c=true;
    
        try
        {
            update MemberToUpdate;
        }
        catch(Exception e)
        {
            ApexPages.addMessages(e);
        }
        MemberToUpdate=null;
        return null;
    }
        
    private Missional_Community__c GetGroupByArenaId(String arenaGroupId)
    {
        List<Missional_Community__c> groups=[select id, group_name__c from Missional_Community__c where arena_group_id__c=:arenaGroupId limit 1];
        if(groups.size()>0)
            return groups[0];
        return null;
    }
}